﻿// (c) Norbert Huffschmid
// This source is subject to the Microsoft Public License (Ms-PL).
// Please see http://go.microsoft.com/fwlink/?LinkID=131993 for details.

using System.Collections.Generic;
using System.ComponentModel;
using System.Windows.Controls;
using System;

namespace SpinDotters.Widgets
{
    /// <summary>
    /// View model part of DatePicker control
    /// </summary>
    public partial class DatePicker
    {
        private int dayHigh;
        private int dayLow;
        private int monthNameIndex;
        private int year;


        /// <summary>
        /// Gets a list of day names.
        /// </summary>
        public IList<string> DayNames
        {
            get { return this.dayNames; }
        }

        /// <summary>
        /// Gets the current day name index.
        /// </summary>
        public int DayNameIndex
        {
            get { return (int)this.SelectedDate.Value.DayOfWeek; }
        }

        /// <summary>
        /// Gets or sets the day's high part.
        /// </summary>
        public int DayHigh
        {
            get { return this.dayHigh; }
            set
            {
                this.dayHigh = value;
                SetSelectedDate();
            }
        }

        /// <summary>
        /// Gets or sets the day's low part.
        /// </summary>
        public int DayLow
        {
            get { return this.dayLow; }
            set
            {
                this.dayLow = value;
                SetSelectedDate();
            }
        }

        /// <summary>
        /// Gets a list of month names.
        /// </summary>
        public IList<string> MonthNames
        {
            get { return this.monthNames; }
        }

        /// <summary>
        /// Gets or sets the month name index.
        /// </summary>
        public int MonthNameIndex
        {
            get { return this.monthNameIndex; }
            set
            {
                this.monthNameIndex = value;
                SetSelectedDate();
            }
        }

        /// <summary>
        /// Gets the year minimum.
        /// </summary>
        public int YearMinimum
        {
            get { return this.DisplayDateStart.Year; }
        }

        /// <summary>
        /// Gets or sets the year.
        /// </summary>
        public int Year
        {
            get { return this.year; }
            set
            {
                this.year = value;
                SetSelectedDate();
            }
        }

        /// <summary>
        /// Gets the year maximum.
        /// </summary>
        public int YearMaximum
        {
            get { return this.DisplayDateEnd.Year; }
        }


        private void SetModelValues()
        {
            if (this.SelectedDate != null)
            {
                this.dayHigh = this.SelectedDate.Value.Day / 10;
                this.dayLow = this.SelectedDate.Value.Day % 10;
                this.monthNameIndex = this.SelectedDate.Value.Month - 1;
                this.year = this.SelectedDate.Value.Year;
            }
            else
            {
                this.dayHigh = 0;
                this.dayLow = 1;
                this.monthNameIndex = 0;
                this.year = DateTime.Now.Year;
            }
        }

        private void SetSelectedDate()
        {
            try
            {
                this.SelectedDate = new DateTime(this.year, this.monthNameIndex + 1, this.dayHigh * 10 + this.dayLow);
            }
            catch (ArgumentOutOfRangeException)
            {
                this.SelectedDate = null;
            }
        }
    }
}
